<?php


namespace app\shop\controller\admin;


use app\enterprise\model\User;
use app\shop\controller\BaseAdminController;
use app\shop\model\user\GroupModel;
use app\shop\model\user\GroupUserModel;
use app\shop\model\user\UserModel;
use utils\Aes;

class GroupController extends BaseAdminController
{

    public function setModel()
    {
        // TODO: Implement setModel() method.
    }

    function setSearch()
    {
        $where = [];
        $mobile = $this->request->param('mobile', '', 'trim');
        if ($mobile) {
            $userId = UserModel::where('mobile', $mobile)->value('user_id');
            $groupIdArr = GroupUserModel::where('user_id', $userId)->column('group_id');
            $where[] = ['group_id', 'in', $groupIdArr];
        }
        return [
            'like' => ['name'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    /**
     * 获取群聊列表
     * @return \think\response\Json
     */
    public function getGroupList()
    {
        try {
            $where = $this->searchWhere;
            $list = GroupModel::where($where)
                ->order($this->orderBy, $this->sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['create_time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $v['create_user_name'] = UserModel::where('user_id', $v['create_user'])->value('realname', '');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取添加群聊的所有用户
     * @return \think\response\Json
     */
    public function getAllUser()
    {
        try {
            $groupId = $this->request->param('group_id', 0, 'int');
            if (!$groupId) {
                $list = UserModel::where('status', 1)->field('user_id,realname')->select();
                foreach ($list as &$value) {
                    $value['key'] = $value['user_id'];
                    $value['label'] = $value['realname'];
                    $value['disabled'] = false;
                }
            } else {
                $groupUserIdArr = GroupUserModel::where('group_id', $groupId)->column('user_id');
                $list = UserModel::where('status', 1)->field('user_id,realname')->select();
                foreach ($list as &$value) {
                    $value['key'] = $value['user_id'];
                    $value['label'] = $value['realname'];
                    $value['disabled'] = in_array($value['user_id'], $groupUserIdArr);
                }
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function setNamespace()
    {
        // TODO: Implement setNamespace() method.
    }

    public function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }
}